//
//  TJCollectMoneyController.m
//  Teleport
//
//  Created by hc-101 on 2020/6/10.
//  Copyright © 2020 xtjun. All rights reserved.
//
//收款
#import "TJCollectMoneyController.h"
#import "JXCategoryView.h"
#import "TJTransferRecordListController.h"

@interface TJCollectMoneyController ()<JXCategoryViewDelegate>

@property (nonatomic, strong) JXCategoryTitleView *categoryView;
@property (weak, nonatomic) UIImage *QRImage;
/**标题数组 **/
@property (strong, nonatomic) NSArray *coinTypes;
@property (weak, nonatomic) UIImageView *QRImageView;


@end

@implementation TJCollectMoneyController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _coinTypes = @[@"BTC", @"USDT"];
    
    self.navigationBar.rightBarButton = [TJButtonBarButton barButtonWithTitle:@"资产记录".localizedString color:UIColor.whiteColor target:self action:@selector(didClickNavBarRightButton)];
    
    self.navigationBar.backgroundColor = UIColor_Hex(0x2C6CC7);
    self.navigationBar.lineView.hidden = YES;
    [self.navigationBar backBarButtonWhiteColor];
    self.navigationBar.titleTextColor = UIColor.whiteColor;
    self.navigationBar.title = @"收款".localizedString;
    
    UIView *topView = [UIView tj_addToView:self.view];
    topView.backgroundColor = UIColor_Hex(0x2C6CC7);
    topView.frame = CGRectMake(0, KNavBarHight, KScreenWidth, 180);
    
    self.view.backgroundColor = UIColor.whiteColor;
    self.categoryView.backgroundColor = UIColor.whiteColor;
    self.categoryView.layer.cornerRadius = 15;
    self.categoryView.layer.masksToBounds = YES;
    self.categoryView.titles = _coinTypes;
    self.categoryView.delegate = self;
    self.categoryView.titleFont = UIFont_Px(16);
    self.categoryView.titleColor = UIColor_Hex(0x2C6CC7);
    self.categoryView.titleSelectedColor = [UIColor whiteColor];
    
    self.categoryView.titleLabelMaskEnabled = YES;
    self.categoryView.selectedAnimationEnabled = NO;
    self.categoryView.cellSpacing = 10;
    self.categoryView.cellWidth = 50;
    [topView addSubview:self.categoryView];
    self.categoryView.frame = CGRectMake((KScreenWidth - 130) * 0.5, 16, 130, 30);
    //指示器样式
    JXCategoryIndicatorBackgroundView *backgroundView = [[JXCategoryIndicatorBackgroundView alloc] init];
    backgroundView.indicatorHeight = 24;
    backgroundView.indicatorColor = UIColor_Hex(0x2C6CC7);
    self.categoryView.indicators = @[backgroundView];
    //二维码bgView
    UIView *QRCodeView = [UIView tj_addToView:self.view];
    QRCodeView.backgroundColor = UIColor.whiteColor;
    [QRCodeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.centerY.equalTo(topView.mas_bottom).offset(20);
        make.height.width.offset(ZOOM(170));
    }];
    
    
    NSString *resultString = [self QRCodeContentStringWithCoinName:_coinTypes.firstObject coinNo:@"1"];
    UIImageView *QRImageView = [UIImageView tj_addToView:QRCodeView];
    [Tools createQRWithString:resultString QRSize:CGSizeMake(ZOOM(165), ZOOM(165)) finishBlock:^(UIImage *image) {
        QRImageView.image = image;
        self.QRImage = image;
    }];
    [QRImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.offset(4);
        make.right.bottom.offset(-4);
    }];
    
    _QRImageView = QRImageView;
    
    UILabel *userIdLabel = [UILabel lableWithSuperView:self.view font:UIFont_Px(14) color:UIColor_Hex(0x2C6CC7) title:TJUserInfo.readInfo.uuid textAlignment:NSTextAlignmentLeft];
    [userIdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(QRImageView.mas_bottom).offset(38);
        make.centerX.offset(0);
    }];
    
    UIButton *copyButton = [UIButton buttonWithSuperView:self.view font:UIFont_Px(14) color:UIColor.whiteColor title:@"复制地址".localizedString];
    copyButton.backgroundColor = UIColor_Hex(0x2C6CC7);
    [copyButton shearRoundedCornersWithRadiu:3];
    [copyButton addTarget:self action:@selector(didClickCopyButton) forControlEvents:UIControlEventTouchUpInside];
    [copyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-100);
        make.left.offset(100);
        make.height.offset(44);
        make.centerX.offset(0);
        make.top.equalTo(userIdLabel.mas_bottom).offset(25);
    }];
    
}
- (void)didClickCopyButton {
    [Tools copyStringToPasteboard:TJUserInfo.readInfo.uuid];
}

- (void)didClickNavBarRightButton {
    TJTransferRecordListController *vc = [TJTransferRecordListController new];
       [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark ---------------JXCategoryViewDelegate------------------
//点击选中或者滚动选中都会调用该方法
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
    NSString *resultString = [self QRCodeContentStringWithCoinName:_coinTypes[index] coinNo:@(index + 1).stringValue];
    [Tools createQRWithString:resultString QRSize:CGSizeMake(ZOOM(165), ZOOM(165)) finishBlock:^(UIImage *image) {
        self.QRImageView.image = image;
        self.QRImage = image;
    }];
}
//滚动选中的情况才会调用该方法
- (void)categoryView:(JXCategoryBaseView *)categoryView didScrollSelectedItemAtIndex:(NSInteger)index {
    NSString *resultString = [self QRCodeContentStringWithCoinName:_coinTypes[index] coinNo:@(index + 1).stringValue];
    [Tools createQRWithString:resultString QRSize:CGSizeMake(ZOOM(165), ZOOM(165)) finishBlock:^(UIImage *image) {
        self.QRImageView.image = image;
        self.QRImage = image;
    }];
}
//二维码内容
- (NSString *)QRCodeContentStringWithCoinName:(NSString *)coinName coinNo:(NSString *)coinNo {
    NSDictionary *parameter = @{
        @"uuid": TJUserInfo.readInfo.uuid,
        @"coinName": coinName,
        @"coinNo": coinNo,
        @"type": @"transfer",
        @"versionName": [Tools appVersion],
    };
    TJLog(@"%@", parameter);
    NSMutableString *resultString = QRCodePrefix.mutableCopy;
    [resultString appendString:[Tools convertToJsonString:parameter]];
    TJLog(@"%@", resultString);
    return resultString;
}
- (JXCategoryTitleView *)categoryView {
    if (_categoryView == nil) {
        _categoryView = [[JXCategoryTitleView alloc] init];
    }
    return _categoryView;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

@end
